//
//  JuOperation.swift
//  JuSwift
//
//  Created by Juvid on 2018/6/7.
//  Copyright © 2018年 Juvid. All rights reserved.
//


class JuOperation: NSObject {
    func juStart()  {
        let vector1 = Vector2D.init(x: 4, y: 5)
        let vector2 = Vector2D.init(x: 2, y: 1)
        let vector = vector1 + vector2
        var negative = -vector
        negative += vector

        var toBeDoubled = Vector2D(x: 1.0, y: 4.0)
        let afterDoubling = +++toBeDoubled
    }
}



//func func <<< (test1:String,test2:String) {
//
//}


prefix operator +++ ///< 自定义前缀运算符
infix operator +-: AdditionPrecedence///< 自定义中缀运算符 （此运算符属于AdditionPrecedence优先组必须有两个参数）
infix operator <<< : AssignmentPrecedence
//postfix operator >>>

// MARK:- Objects with Basic typesrn Vector2D(x: left.x + right.x, y: left.y + right.y)



struct Vector2D {
    var x = 0.0, y = 0.0
}
//prefix operator <<<
extension Vector2D {
    static func + (left: Vector2D, right: Vector2D) -> Vector2D {
        return Vector2D(x: left.x + right.x, y: left.y + right.y)
    }
    static prefix func - (vector: Vector2D) -> Vector2D {
        return Vector2D(x: -vector.x, y: -vector.y)
    }
    static func += (left: inout Vector2D, right: Vector2D) {
        left = left + right
    }

    /*
     static public func += <T> (left: inout T!,next : Vector2D){

    }
    static public func += <T> (left: inout T,next : Vector2D){

    }
     */
    static func <<< (left: inout Vector2D, right: Vector2D) {
        left = left + right
    }
    static prefix func +++ (vector: inout Vector2D) -> Vector2D {
        vector += vector
        return vector
    }
    static func +- (left: Vector2D, right: Vector2D) -> Vector2D {
        return Vector2D(x: left.x + right.x, y: left.y - right.y)
    }
}
/**
 inout的本质总结

 如果实参是有物理内存地址，且没有设置属性观察器

 直接将实参的内存地址传入函数（实参进行引用传递）

 如果实参是计算属性 或 设置了属性观察器

 采用了Copy In Copy Out的做法

 调用函数时，先复制实参的值，产生副本【get】
 将副本的内存地址传入函数（副本进行引用传递），在函数内部可以修改副本的值
 函数返回后，再将副本的值覆盖实参的值【set】
 总结：inout的本质就是引用传递（地址传递）
 */
